package com.atguigu.gmall.realtime.common.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class IkUtil {

    public static List<String> IKSplit(String keywords) {
        StringReader stringReader = new StringReader(keywords);
        IKSegmenter ikSegmenter = new IKSegmenter(stringReader, true);
        ArrayList<String> result = new ArrayList<>();
        try {
            Lexeme next = ikSegmenter.next();

            while(next != null) {
                result.add(next.getLexemeText());
                next = ikSegmenter.next();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return result;
    }

    public static void main(String[] args) throws IOException {
        String s = "佛挡杀佛递四方速递放水阀算法的沙发沙发上";

        StringReader stringReader = new StringReader(s);

        IKSegmenter ikSegmenter = new IKSegmenter(stringReader, true);

        Lexeme next = ikSegmenter.next();

        while(next != null) {
            System.out.println(next.getLexemeText());
            next = ikSegmenter.next();
        }
    }
}
